package com.sc.test.recyclerview;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Shen Chao on 2018-01-19-019 下午 13:58.
 */

public class ItemDivider extends RecyclerView.ItemDecoration {
    private int dividerWith = 1;
    private Paint paint;
    private RecyclerView.LayoutManager layoutManager;

    public ItemDivider() {
        initPaint();
    }

    private void initPaint() {
        if (paint == null) {
            //抗锯齿
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            //填充模式，内容和边框
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
        }
    }

    public ItemDivider setDivederWith(int dividerWith) {
        this.dividerWith = dividerWith;
        return this;
    }

    public ItemDivider setDividerColor(int color) {
        initPaint();
        paint.setColor(color);
        return this;
    }

    /**
     * 绘制item宽度和高度
     *
     * @param outRect
     * @param view
     * @param parent
     * @param state
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (layoutManager == null) {
            layoutManager = parent.getLayoutManager();
        }
        if (layoutManager instanceof LinearLayoutManager) {
            int orientation = ((LinearLayoutManager) layoutManager).getOrientation();
            if (orientation == LinearLayoutManager.HORIZONTAL) {
                //分割线在右侧
                outRect.right = dividerWith;
            } else if (orientation == LinearLayoutManager.VERTICAL) {
                //分割线在底部
                outRect.bottom = dividerWith;
            }
            if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
                //如果是gridlayoutmanager 需要绘制另一个方向
                if (orientation == LinearLayoutManager.VERTICAL && lp != null && lp.getSpanIndex() > 0) {
                    //如果是垂直方向 最左侧略过
                    outRect.left = dividerWith;
                } else if (orientation == LinearLayoutManager.HORIZONTAL && lp != null && lp.getSpanIndex() > 0) {
                    //水平方向，最上边略过
                    outRect.top = dividerWith;
                }
            }
        }
    }

    /**
     * 在item绘制之前调用 在item下边
     *
     * @param c
     * @param parent
     * @param state
     */
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        //处理交叉位置的空隙
        int offSet = (int) Math.ceil(dividerWith / 2);
        for (int i = 0; i < parent.getChildCount(); i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int left1 = child.getRight() + params.rightMargin;
            int right1 = left1 + dividerWith;
            int top1 = child.getTop() - offSet - params.topMargin;
            int bottom1 = child.getBottom() + offSet + params.bottomMargin;
            System.out.println("left1---" + left1);
            System.out.println("right1---" + right1);
            System.out.println("top1---" + top1);
            System.out.println("bottom1---" + bottom1);
            //绘制分割线(矩形)
            c.drawRect(left1, top1, right1, bottom1, paint);
            int left2 = child.getLeft() - offSet - params.leftMargin;
            int right2 = child.getRight() + offSet + params.rightMargin;
            int top2 = child.getBottom() + params.bottomMargin;
            int bottom2 = top2 + dividerWith;
            System.out.println("left2---" + left2);
            System.out.println("right2---" + right2);
            System.out.println("top2---" + top2);
            System.out.println("bottom2---" + bottom2);
            //绘制分割线(矩形)
            c.drawRect(left2, top2, right2, bottom2, paint);
        }
    }

    /**
     * 在item绘制后调用 在item上边
     *
     * @param c
     * @param parent
     * @param state
     */
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }
}
